#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：zhcx-service-test 
@File    ：interface.py
@Author  ：关依林
@Date    ：2024/1/22 16:52 
'''
import requests
import json

def getFbmPrice(fbmSet):
    lis = []
    skuSet = [i["sku"] for i in fbmSet]
    url = "https://erpapi.zhcxkj.com/Amazon/Warehouse/SelfShipmentWarehouseIdList"

    payload = json.dumps({
        "input": {
            "customerID": 1,
            "skuList": skuSet
        },
        "language": ""
    })
    headers = {
        'Authorization': 'Basic YXBpdXNlcjpFN0kxLWxJX2QjOGRscw==',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()["data"]
    for i in response:
        sku,wareHouseId,wareHouseType = i["sku"],i["wareHouseId"],i["wareHouseType"]
        listingSet = list(filter(lambda x:x["sku"] == sku, fbmSet))
        for j in listingSet:
            j["wareHouseId"] = wareHouseId
            j["wareHouseType"] = wareHouseType
        lis += listingSet
    return lis

def getFbaPrice(fbaSet, sourceId):
    lis = []
    for i in fbaSet:
        temp = {
                "customerID": 1,
                "sku": i["sku"],
                "orderSourceSKU": i["msku"],
                "orderSourceID": sourceId
            }
        lis.append(temp)
    url = "https://erpapi.zhcxkj.com/Amazon/Warehouse/GetFBAWareHouseList"
    payload = json.dumps({
        "source": [
            ""
        ],
        "input": lis,
        "language": ""
    })
    headers = {
        'Authorization': 'Basic YXBpdXNlcjpFN0kxLWxJX2QjOGRscw==',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()["data"]
    lis = []
    for i in response:
        sku,wareHouseId = i["sku"],i["wareHouseId"]
        listingSet = list(filter(lambda x:x["sku"] == sku, fbaSet))
        for j in listingSet:
            j["wareHouseId"] = wareHouseId
            # j["wareHouseType"] = 1
        lis += listingSet
    return lis


def CalculatorPriceAPI(listingSet,country,sourceId,businessTypeCode):
    url = "http://priceapi-amazon-fenji-tiaojia.zhcxkj.com/api/CalculationFormula/CalculationPrice/GetCalculatorPriceList"
    lis = []
    for i in listingSet:
        temp = {
                "customerId": 1,
                "businessTypeCode": businessTypeCode,
                "platformId": 1,
                "warehouseTypeId": 1,
                "warehouseId": i["wareHouseId"],
                "orderSourceId": sourceId,
                "Country": country,
                "sku": i["sku"]
            }
        lis.append(temp)

    payload = json.dumps({
        "language": None,
        "input": lis
    })
    headers = {
        'Authorization': 'Basic aXJvYm90Ym94OmkxcjVvOEJvdGJveA==',
        'Content-Type': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload).json()["data"]
    lis = []
    for i in response:
        sku,price = i["Sku"],i["Price"]
        temp = list(filter(lambda x: x["sku"] == sku, listingSet))
        for j in temp:
            j["price"] = price
        lis += temp
    return lis


